# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free 
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
#    MA  02110-1301  USA

import curses
import opt_showContactInfo, opt_chgNick, opt_quit, opt_addContact, \
	opt_emoticon, opt_addGroup, opt_delGroup, opt_delContact, \
	opt_renGroup, opt_renContact, opt_moveContact, opt_buContact, \
	opt_buGroup, opt_chgStatus, opt_flnMsg
import gettext
_ = gettext.gettext

def manageInput(mam, carac):
	ui = mam.ui
	m = mam.m

	# If We are in an options win.
	if ui.options_win:
		if ui.options_list == ui.options.optToList(_('Show contact info')) \
		and ui.options_index == ui.options.optToIndex(_('Show contact info')):
			opt_showContactInfo.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Change nick')) \
		and ui.options_index == ui.options.optToIndex(_('Change nick')):
			opt_chgNick.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Quit')) \
		and ui.options_index == ui.options.optToIndex(_('Quit')):
			opt_quit.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Delete group')) \
		and ui.options_index == ui.options.optToIndex(_('Delete group')):
			opt_delGroup.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Add contact')) \
		and ui.options_index == ui.options.optToIndex(_('Add contact')):
			opt_addContact.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Add group')) \
		and ui.options_index == ui.options.optToIndex(_('Add group')):
			opt_addGroup.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Rename group')) \
		and ui.options_index == ui.options.optToIndex(_('Rename group')):
			opt_renGroup.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Delete contact')) \
		and ui.options_index == ui.options.optToIndex(_('Delete contact')):
			opt_delContact.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Rename contact')) \
		and ui.options_index == ui.options.optToIndex(_('Rename contact')):
			opt_renContact.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Block / Unblock contact')) \
		and ui.options_index == ui.options.optToIndex(_('Block / Unblock contact')):
			opt_buContact.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Block / Unblock group')) \
		and ui.options_index == ui.options.optToIndex(_('Block / Unblock group')):
			opt_buGroup.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Insert emoticon')) \
		and ui.options_index == ui.options.optToIndex(_('Insert emoticon')):
			opt_emoticon.handleOption(ui, ui.winOpt2.manageInput(carac))
		elif ui.options_list == ui.options.optToList(_('Move contact to group')) \
		and ui.options_index == ui.options.optToIndex(_('Move contact to group')):
			opt_moveContact.handleOption(ui, ui.winOpt2.manageInput(carac))
		# FlnMsg
		else:
			opt_flnMsg.handleOption(ui, ui.winOpt2.manageInput(carac))

	# pressed ENTER key
	elif carac in [int(0xa), curses.KEY_RIGHT]:
		# If menu option is disabled
		if ui.options.list[ui.options.optToListIndex(ui.options_list, ui.options_index)].available == 0:
			return

		# entered in "Change status" menu
		if ui.options_list == ui.options.optToList(_('Change status')) \
		and ui.options_index == ui.options.optToIndex(_('Change status')):
			ui.winOptMenu.erase()
			ui.winOptMenu.refresh()
			ui.options_list = ui.options_index + 1
			ui.options_index = 0
			ui.refreshScr()

		# selected "Change nick" option
		elif ui.options_list == ui.options.optToList(_('Change nick')) \
		and ui.options_index == ui.options.optToIndex(_('Change nick')):
			opt_chgNick.showWin(mam)
			
		# selected File "transfer" option
		#elif ui.options_list == ui.options.optToList(_('Files transfer')) \
		#and ui.options_index == ui.options.optToIndex(_('Files transfer')):
		#	ui.winOptMenu.erase()
		#	ui.winOptMenu.refresh()
		#	ui.options_list = ui.options_index + 1
		#	ui.options_index = 0
		#	ui.ShowOptionsWin(ui.options_list, ui.options_index)
			
		# selected "List" option
		#elif ui.options_list == ui.options.optToList(_('Show lists')) \
		#and ui.options_index == ui.options.optToIndex(_('Show lists')):
		#	ui.winOptMenu.erase()
		#	ui.winOptMenu.refresh()
		#	ui.options_list = ui.options_index + 1
		#	ui.options_index = 0
		#	ui.ShowOptionsWin(ui.options_list, ui.options_index)

		# selected "Logs" option
		#elif ui.options_list == ui.options.optToList(_('Show logs')) \
		#and ui.options_index == ui.options.optToIndex(_('Show logs')):		
		#	ui.winOptMenu.erase()
		#	ui.winOptMenu.refresh()
		#	ui.options_list = ui.options_index + 1
		#	ui.options_index = 0
		#	ui.ShowOptionsWin(ui.options_list, ui.options_index)

		# entered in "Handle users and groups" menu
		elif ui.options_list == ui.options.optToList(_('Handle contacts/groups')) \
		and ui.options_index == ui.options.optToIndex(_('Handle contacts/groups')):
			if ui.padArray.idactive or mam.contactListManager.default_user == -1:
				# Make "Add group" available
				if int(mam.config['show_groups']):
					ui.options.list[ui.options.nametoIndex(_('Add group'))].available = 1
				else:
					ui.options.list[ui.options.nametoIndex(_('Add group'))].available = 0

				# Make "Rename contact" unavailable
				ui.options.list[ui.options.nametoIndex(_('Rename contact'))].available = 0

				# Make "Block / Unblock contact" unavailable
				ui.options.list[ui.options.nametoIndex(_('Block / Unblock contact'))].available = 0

				# Make "Delete contact" unavailable
				ui.options.list[ui.options.nametoIndex(_('Delete contact'))].available = 0
	
				# Make "Move contact to group" unavailable
				ui.options.list[ui.options.nametoIndex(_('Move contact to group'))].available = 0

				# Make "Show contact info" unavailable
				ui.options.list[ui.options.nametoIndex(_('Show contact info'))].available = 0
				
				if not ui.padArray.idactive and int(mam.config['show_groups']):
					# Make "Block group" available
					ui.options.list[ui.options.nametoIndex(_('Block / Unblock group'))].available = 1

					# Make "Rename group" available
					ui.options.list[ui.options.nametoIndex(_('Rename group'))].available = 1

					# Make "Delete group" available
					ui.options.list[ui.options.nametoIndex(_('Delete group'))].available = 1
			else:
				# Make "Show contact info" available
				ui.options.list[ui.options.nametoIndex(_('Show contact info'))].available = 1

				# Make "Rename contact" available
				ui.options.list[ui.options.nametoIndex(_('Rename contact'))].available = 1

				# Make "Block / Unblock contact" available
				ui.options.list[ui.options.nametoIndex(_('Block / Unblock contact'))].available = 1

				# Make "Delete contact" available
				ui.options.list[ui.options.nametoIndex(_('Delete contact'))].available = 1

				# Make "Block group" unavailable
				ui.options.list[ui.options.nametoIndex(_('Block / Unblock group'))].available = 0

				# Make "Rename group" unavailable
				ui.options.list[ui.options.nametoIndex(_('Rename group'))].available = 0

				# Make "Delete group" unavailable
				ui.options.list[ui.options.nametoIndex(_('Delete group'))].available = 0

				# Make "Move contact to group" available
				if int(mam.config['show_groups']):
					ui.options.list[ui.options.nametoIndex(_('Move contact to group'))].available = 1
				# Make "Move contact to group" unavailable
				else:
					ui.options.list[ui.options.nametoIndex(_('Move contact to group'))].available = 0

			ui.winOptMenu.erase()
			ui.winOptMenu.refresh()
			ui.options_list = ui.options_index + 1
			ui.options_index = 0
			ui.ShowOptionsWin(ui.options_list, ui.options_index)
			ui.padInput.refresh()

		# block or unblock contact
		elif ui.options_list == ui.options.optToList(_('Block / Unblock contact')) \
		and ui.options_index == ui.options.optToIndex(_('Block / Unblock contact')) \
		and not ui.padArray.idactive:
			opt_buContact.showWin(mam)
		
		# block or unblock group
		elif ui.options_list == ui.options.optToList(_('Block / Unblock group')) \
		and ui.options_index == ui.options.optToIndex(_('Block / Unblock group')) \
		and not ui.padArray.idactive:
			opt_buGroup.showWin(mam)
	
		# selected Show contact info option
		elif ui.options_list == ui.options.optToList(_('Show contact info')) \
		and ui.options_index == ui.options.optToIndex(_('Show contact info')):
			if ui.mam.contactListManager.default_user == -1:
				return
			opt_showContactInfo.showWin(mam)

		# add a new contact
		elif ui.options_list == ui.options.optToList(_('Add contact')) \
		and ui.options_index == ui.options.optToIndex(_('Add contact')):
			opt_addContact.showWin(mam)

		# add a new group
		elif ui.options_list == ui.options.optToList(_('Add group')) \
		and ui.options_index == ui.options.optToIndex(_('Add group')):
			opt_addGroup.showWin(mam)
	
		# rename a contact
		elif ui.options_list == ui.options.optToList(_('Rename contact')) \
		and ui.options_index == ui.options.optToIndex(_('Rename contact')) \
		and not ui.padArray.idactive:
			if ui.mam.contactListManager.default_user == -1:
				return
			opt_renContact.showWin(mam)

		# delete a contact
		elif ui.options_list == ui.options.optToList(_('Delete contact')) \
		and ui.options_index == ui.options.optToIndex(_('Delete contact')) \
		and not ui.padArray.idactive:
			if ui.mam.contactListManager.default_user == -1:
				return
			opt_delContact.showWin(mam)

		# move a contact to group
		elif ui.options_list == ui.options.optToList(_('Move contact to group')) \
		and ui.options_index == ui.options.optToIndex(_('Move contact to group')) \
		and not ui.padArray.idactive:
			if ui.mam.contactListManager.default_user == -1:
				return
			opt_moveContact.showWin(mam)

		# insert emoticon
		elif ui.options_list == ui.options.optToList(_('Insert emoticon')) \
		and ui.options_index == ui.options.optToIndex(_('Insert emoticon')):
		#and ui.padArray.idactive not in (0,1):
			opt_emoticon.showWin(mam)

		# rename a group
		elif ui.options_list == ui.options.optToList(_('Rename group')) \
		and ui.options_index == ui.options.optToIndex(_('Rename group')) \
		and not ui.padArray.idactive:
			if ui.mam.contactListManager.default_user <> -1:
				return
			opt_renGroup.showWin(mam)

		# delete a group
		elif ui.options_list == ui.options.optToList(_('Delete group')) \
		and ui.options_index == ui.options.optToIndex(_('Delete group')) \
		and not ui.padArray.idactive:
			if ui.mam.contactListManager.default_user <> -1:
				return
			opt_delGroup.showWin(mam)

		# chose a new status
		elif ui.options_list == ui.options.optToList(_('NLN - Online')):
			opt_chgStatus.handleOption(mam)

		# quit
		elif ui.options_list == ui.options.optToList(_('Quit')) \
		and ui.options_index == ui.options.optToIndex(_('Quit')):
			opt_quit.showWin(mam)

	# F4
	elif carac in [int(0x10c), curses.KEY_LEFT]:
		# If I am in Main Menu and want to close it.
		if ui.options_list == 0:
			ui.options_index = -1
			ui.options_list = 0
			ui.winOptMenu.erase()
			ui.winOptMenu.refresh()
			del ui.winOptMenu
			ui.refreshScr()

		# If I am in Change Status, and want to return to main menu
		elif ui.options_list == ui.options.optToIndex(_('Change status')) + 1:
			ui.winOptMenu.erase()
			ui.winOptMenu.refresh()
			ui.options_list = 0
			ui.options_index = ui.options.optToIndex(_('Change status'))
			ui.ShowOptionsWin(ui.options_list, ui.options_index)
			ui.padInput.refresh()

		# If I am in Manage User and Groups menu, and want to return to main menu
		elif ui.options_list == ui.options.optToIndex(_('Handle contacts/groups')) + 1:
			ui.winOptMenu.erase()
			ui.winOptMenu.refresh()
			ui.options_list = 0
			ui.options_index = ui.options.optToIndex(_('Handle contacts/groups'))
			if ui.padArray.idactive == 0:
				ui.rebuild_contactList_whileinMenu()
			else:
				ui.rebuild_tab_whileinMenu()
			ui.ShowOptionsWin(ui.options_list, ui.options_index)
			ui.padInput.refresh()

		# If I am in File transfer menu, and want to return to main menu
		elif ui.options_list == ui.options.optToIndex(_('Files transfer')) + 1:
			ui.winOptMenu.erase()
			ui.winOptMenu.refresh()
			ui.options_list = 0
			ui.options_index = ui.options.optToIndex(_('Files transfer'))
			ui.ShowOptionsWin(ui.options_list, ui.options_index)
			ui.padInput.refresh()

		# If I am in List menu, and want to return to main menu
		elif ui.options_list == ui.options.optToIndex(_('Show lists')) + 1:
			ui.winOptMenu.erase()
			ui.winOptMenu.refresh()
			ui.options_list = 0
			ui.options_index = ui.options.optToIndex(_('Show lists'))
			ui.ShowOptionsWin(ui.options_list, ui.options_index)
			ui.padInput.refresh()

		# If I am in Logs menu, and want to return to main menu
		elif ui.options_list == ui.options.optToIndex(_('Show logs')) + 1:
			ui.winOptMenu.erase()
			ui.winOptMenu.refresh()
			ui.options_list = 0
			ui.options_index = ui.options.optToIndex(_('Show logs'))
			ui.ShowOptionsWin(ui.options_list, ui.options_index)
			ui.padInput.refresh()

	# pressed UpArrow
	elif carac == curses.KEY_UP:
		if ui.options_win == 1:
			return
		if ui.options_index <> 0:
			ui.options_index -= 1
			if ui.options.list[ui.options.optToListIndex(ui.options_list, ui.options_index)].isLine:
				ui.options_index -= 1
		else:
			#return
			ui.options_index = ui.options.size(ui.options_list) - 1
		ui.ShowOptionsWin(ui.options_list, ui.options_index)
		ui.padInput.refresh()
			
	# pressed DownArrow
	elif carac == curses.KEY_DOWN:
		if ui.options_win == 1:
			return
		if ui.options_index <> ui.options.size(ui.options_list) - 1:
			ui.options_index += 1			
			if ui.options.list[ui.options.optToListIndex(ui.options_list, ui.options_index)].isLine:
				ui.options_index += 1
		else:
			#return
			ui.options_index = 0
		ui.ShowOptionsWin(ui.options_list, ui.options_index)
		ui.padInput.refresh()
			
	# pressed END
	elif carac == curses.KEY_END or carac == curses.KEY_NPAGE:
		if ui.options_win == 1:
			return
		ui.options_index = ui.options.size(ui.options_list) - 1
		ui.ShowOptionsWin(ui.options_list, ui.options_index)
		ui.padInput.refresh()

	# pressed HOME
	elif carac == curses.KEY_HOME or carac == curses.KEY_PPAGE:
		if ui.options_win == 1:
			return
		ui.options_index = 0
		ui.ShowOptionsWin(ui.options_list, ui.options_index)
		ui.padInput.refresh()
